package org.csu.mypetstoreplus.service;

import org.csu.mypetstoreplus.entity.*;
import org.csu.mypetstoreplus.persistence.*;
import org.csu.mypetstoreplus.vo.AccountVO;
import org.csu.mypetstoreplus.util.Md5Util;
import org.csu.mypetstoreplus.util.ThreadLocalUtil;
import org.csu.mypetstoreplus.vo.MyOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AccountService {

    @Autowired
    private SignOnDao signOnDao;
    @Autowired
    private ProfileDao profileDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderStatusDao orderStatusDao;

    public AccountVO getAccountVOByUsername(String username) {
        Account account = accountDao.findByUsername(username);
        SignOn signOn = signOnDao.findByUsername(username);
        Profile profile = profileDao.findByUsername(username);
        return new AccountVO(account, signOn, profile);  //返回AccountVO对象
    }
    public SignOn getSignOnByUsername(String username) {
        return signOnDao.findByUsername(username);  //返回SignOn对象
    }

    public void register(AccountVO accountVO) {
        //MD5加密
        String md5String = Md5Util.getMD5String(accountVO.getPassword());
        accountVO.setPassword(md5String);

        //在account表里添加一条记录
        Account account = new Account(accountVO);
        accountDao.save(account);
        //在signon表里添加一条记录
        SignOn signOn = new SignOn(accountVO);
        signOnDao.save(signOn);
        //在profile表里添加一条记录
        Profile profile = new Profile(accountVO);
        profileDao.save(profile);
    }

    public void updatePassword(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        //更新signon表
        SignOn signOn = signOnDao.findByUsername(username);
        signOn.setPassword(Md5Util.getMD5String(newPwd));
        signOnDao.save(signOn);
    }

    public void updateAccount(AccountVO accountVO) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        //更新account表
        Account account = accountDao.findByUsername(username);
        account.setEmail(accountVO.getEmail());
        account.setFirstName(accountVO.getFirstName());
        account.setLastName(accountVO.getLastName());
        account.setStatus(accountVO.getStatus());
        account.setAddress1(accountVO.getAddress1());
        account.setAddress2(accountVO.getAddress2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());
        accountDao.save(account);
        //更新profile表
        Profile profile = profileDao.findByUsername(username);
        profile.setFavouriteCategoryId(accountVO.getFavouriteCategoryId());
        profile.setLanguagePreference(accountVO.getLanguagePreference());
        profile.setBannerOption(accountVO.isBannerOption());
        profile.setListOption(accountVO.isListOption());
        profileDao.save(profile);
    }

    public List<MyOrderVO> getOrdersByUsername(String username) {
        List<Order> orders = orderDao.findByUsername(username);
        List<MyOrderVO> myOrderVOs = new ArrayList<MyOrderVO>();
        for (Order order : orders) {
            OrderStatus orderStatus = orderStatusDao.findByOrderId(order.getOrderId());
            MyOrderVO myOrderVO = new MyOrderVO(order, orderStatus);
            myOrderVOs.add(myOrderVO);
        }
        return myOrderVOs;
    }
}
